package com.md.infrastructure.persistence.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.md.base.BaseRepositoryImpl;
import com.md.domain.model.bo.QueryUsersBO;
import com.md.domain.model.po.UsersPO;
import com.md.domain.repository.UsersRepository;
import com.md.infrastructure.persistence.mapper.UsersMapper;
import com.md.util.ObjectUtil;
import org.springframework.stereotype.Repository;

/**
 * @author 刘骄阳
 * 2022-06-12 1:22
 */
@Repository
public class DAOUsersRepository extends BaseRepositoryImpl<UsersPO, UsersMapper> implements UsersRepository {
  public DAOUsersRepository(final UsersMapper mapper) {
    super(mapper);
  }

  @Override
  public UsersPO queryUserByPhone(final String phone) {
    return mapper.selectOne(new LambdaQueryWrapper<UsersPO>().eq(UsersPO::getPhone, phone));
  }

  @Override
  public UsersPO queryUserByAccount(final String account) {
    return mapper.selectOne(new LambdaQueryWrapper<UsersPO>().eq(UsersPO::getAccount, account));
  }

  @Override
  public UsersPO queryUserByName(final String name) {
    return mapper.selectOne(new LambdaQueryWrapper<UsersPO>().eq(UsersPO::getName, name));
  }

  @Override
  public Page<UsersPO> selectListByPage(final QueryUsersBO queryBO) {
    QueryWrapper<UsersPO> wrapper = new QueryWrapper<>();
    wrapper.orderByAsc(ObjectUtil.notEmpty(queryBO.getOrderColum()), queryBO.getOrderColum()).lambda()
      .eq(ObjectUtil.notEmpty(queryBO.getSex()), UsersPO::getSex, queryBO.getSex())
      .eq(ObjectUtil.notEmpty(queryBO.getStatus()), UsersPO::getStatus, queryBO.getStatus())
      .likeRight(ObjectUtil.notEmpty(queryBO.getName()), UsersPO::getName, queryBO.getName());
    return mapper.selectPage(new Page<>(queryBO.getPageNum(), queryBO.getPageSize()), wrapper);
  }
}
